﻿Imports System.ComponentModel.Composition
Imports System.ComponentModel.Composition.Hosting
Imports Contracts
Imports System.IO

''' <summary>
''' Interaction logic for MainAppWindow.xaml
''' </summary>
<Export("MainWindow", GetType(System.Windows.Forms.Form))> _
Partial Public Class MainAppWindow
    Inherits ApplicationWindow
    Implements IWpfHost

    Private _CurrentAddin As IWpfService

    <Import("IWpfService")> _
    Public Property CurrentAddin() As Contracts.IWpfService
        Get
            Return _CurrentAddin
        End Get
        Set(ByVal value As IWpfService)
            _CurrentAddin = value
        End Set
    End Property

    Public Sub New()
        InitializeComponent()

        Application.Current.Compose(Me)

        ' Settings menu item on or off, ShowApp menu item on or off, Wire up callbacks, etc.
        CurrentAddin.Start()

        Me.ApplicationName = "MEF Utility Runner"
        Me.HideOnClose = CurrentAddin.HideOnClose

        ' Sets a custom icon or leaves it as default
        If CurrentAddin.TrayIcon IsNot Nothing Then
            Me.TrayIcon = CurrentAddin.TrayIcon
        End If

        dockPanelAddinInfo.DataContext = CurrentAddin

        AddHandler Application.Current.Exit, AddressOf CurrentApp_Exit
        AddHandler CurrentAddin.StatusUpdated, AddressOf CurrentAddin_StatusUpdated
    End Sub

    ''' <summary>
    ''' Handles status message from a plugin.  This is dispatched to UI thread
    ''' </summary>
    Private Sub CurrentAddin_StatusUpdated(ByVal sender As Object, ByVal e As EventArgs)
        Dispatcher.BeginInvoke(New Action(AddressOf StatusUpdatedHandler))
    End Sub

    ''' <summary>
    ''' Handles status updates on the UI thread by showing a balloon
    ''' </summary>
    Private Sub StatusUpdatedHandler()
        Me.ShowBalloonMessage("Status Update", CurrentAddin.Status)
    End Sub

    ''' <summary>
    ''' Handler for application Exit event.  Gracefully shuts down addin.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub CurrentApp_Exit(ByVal sender As Object, ByVal e As ExitEventArgs)
        CurrentAddin.Stop()
    End Sub

#Region "IWpfHost Members"

    Public Sub RegisterForCallback() Implements Contracts.IWpfHost.RegisterForCallback
        'TODO: Implement RegisterForCallback()
    End Sub

    Public Sub ShowNonModalMessage(ByVal msg As String) Implements Contracts.IWpfHost.ShowNonModalMessage
        Me.ShowBalloonMessage("Message", msg)
    End Sub

#End Region
End Class
